package com.atguigu.flink.chapter05.practice;

import com.atguigu.flink.bean.UserBehavior;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author lzc
 * @Date 2023/6/19 14:55
 */
public class Uv {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        env
            .readTextFile("input/UserBehavior.csv")
            .map(new MapFunction<String, UserBehavior>() {
                @Override
                public UserBehavior map(String value) throws Exception {
                    String[] data = value.split(",");
                    return new UserBehavior(
                        Long.valueOf(data[0]),
                        Long.valueOf(data[1]),
                        Long.valueOf(data[2]),
                        data[3],
                        Long.valueOf(data[4]),
                        1L
                    );
                }
                
            })
            .filter(ub -> "pv".equals(ub.getBehavior()))
            .keyBy(UserBehavior::getBehavior)
            .process(new KeyedProcessFunction<String, UserBehavior, Long>() {
                Set<Long> userIds = new HashSet<Long>();
                
                @Override
                public void processElement(UserBehavior value,
                                           Context ctx,
                                           Collector<Long> out) throws Exception {
                    
                    // 把 userId 的值存入到 Set 集合中, set 集合的长度, 就是 uv
                    /*int pre = userIds.size();
                    userIds.add(value.getUserId());
                    int post = userIds.size();
                    if (post > pre) {
                        out.collect((long) userIds.size());
                    }*/
                    
                    if (userIds.add(value.getUserId())) {
                        out.collect((long) userIds.size());
                    }
                    
                }
            })
            .print();
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*
uv:
    unique visitor 独立访客
    
    
    


 */